package cm.util.action.list;

import java.util.List;

import cm.util.exception.MessageException;

import com.opensymphony.xwork2.ActionSupport;

abstract public class ListActionSupport<E> extends ActionSupport implements
		ListAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4840042774991274290L;

	private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
			.getLog(ListActionSupport.class);

	/**
	 * 跳转参数
	 */
	private String forward;

	/**
	 * 获得默认的分页大小
	 */
	protected int pageSize = 15;

	/**
	 * 总页数
	 */
	protected int totalPages = -1;

	/**
	 * 当前页
	 */
	protected int currentPage = -1;

	/**
	 * 上一页
	 */
	private int previousPage = 1;

	/**
	 * 下一页
	 */
	private int nextPage = 1;

	/**
	 * 总记录条数
	 */
	protected long totalRows = -1;

	/**
	 * 列表数据
	 */
	protected List<E> list;

	/**
	 * 操作类型
	 */
	protected String type;
	
	/**
	 * 起始时间
	 */
	protected String beginDate;
	/**
	 * 结束时间
	 */
	protected String endDate;
	/**
	 * 关键字
	 */
	protected String keyWord;
	public String execute() throws Exception {

		type = getType();
		beginDate = getBeginDate();
		endDate = getEndDate();
		keyWord = getKeyWord();
		if (type == null) {
			throw new MessageException("对不起，操作类型不能为空！");
		}else if (FIND_ALL_DESC_WITH_KEYWORD.equals(type)) {
			forward = findAllDescWithKeyWord(keyWord);
		} 
		else if (FIND_ALL.equals(type)) {
			forward = findAll();
		} else if (FIND_ALL_DESC.equals(type)) {
			forward = findAllDesc();
		}else if (FIND_ALL_DESC_WITH_DATE.equals(type)) {
			if(beginDate==null||endDate==null)
			{
				forward = findAllDesc();
			}
			else{
				forward = findAllDescWithDate(beginDate,endDate);
			}
		} else {
			throw new MessageException("对不起，不存在该操作类型！ type=" + type);
		}
		return forward;
	}

	/**
	 * 初始化页面属性<br>
	 * 必须在已获得totalRows值之后再调用该函数<br>
	 * 调用方式为：<br>
	 * 
	 * 给totalRows赋值<br>
	 * 调用initPageProperties(form)方法<br>
	 * 给list赋值<br>
	 * 调用initAttribute(request)方法<br>
	 * 
	 * 该方法在调用查询语句之前调用<br>
	 * pageSize为系统默认的分页的大小，如要更改pageSize，应在掉用setPageSize方法后再调用该方法<br>
	 * 
	 * 
	 */
	protected void initPageProperties() {

		if (totalRows == -1) {
			log.error("没有初始化totalRows参数！");
		}

		
		currentPage = currentPage <= 1 ? 1 : currentPage;

		totalPages = (totalRows % pageSize == 0) ? ((int) (totalRows / pageSize))
				: ((int) (totalRows / pageSize + 1));

		currentPage = currentPage >= totalPages ? totalPages : currentPage;

		previousPage = currentPage > 1 ? currentPage - 1 : 1;

		nextPage = currentPage < totalPages ? currentPage + 1 : totalPages;

	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotalPages() {
		return totalPages;
	}

	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPreviousPage() {
		return previousPage;
	}

	public void setPreviousPage(int previousPage) {
		this.previousPage = previousPage;
	}

	public int getNextPage() {
		return nextPage;
	}

	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}

	public long getTotalRows() {
		return totalRows;
	}

	public void setTotalRows(long totalRows) {
		this.totalRows = totalRows;
	}

	public List<E> getList() {
		return list;
	}

	public void setList(List<E> list) {
		this.list = list;
	}

	public String getBeginDate() {
		return beginDate;
	}

	public void setBeginDate(String beginDate) {
		this.beginDate = beginDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public String getKeyWord() {
		return keyWord;
	}

	public void setKeyWord(String keyWord) {
		this.keyWord = keyWord;
	}

	/**
	 * 显示所有
	 * 
	 * @return
	 */
	abstract protected String findAll();
	abstract protected String findAllDesc();
	abstract protected String findAllDescWithDate(String beginDate,String endDate);
	abstract protected String findAllDescWithKeyWord(String keyWord);
}
